package com.bi.controller;

import com.bi.common.Result;
import com.bi.service.ExcelImportService;
import com.bi.vo.ImportTaskStatus;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

/**
 * Excel导入控制器
 * 
 * @author BI Platform Team
 * @since 1.0.0
 */
@Slf4j
@Api(tags = "Excel数据导入")
@RestController
@RequestMapping("/excel-import")
public class ExcelImportController {

    @Autowired
    private ExcelImportService excelImportService;

    @ApiOperation("解析Excel文件头部信息")
    @PostMapping("/parse-headers")
    @PreAuthorize("hasAuthority('wide_table:create')")
    public Result<List<String>> parseExcelHeaders(
            @ApiParam("Excel文件") @RequestParam("file") MultipartFile file) {
        
        List<String> headers = excelImportService.parseExcelHeaders(file);
        return Result.success("解析成功", headers);
    }

    @ApiOperation("预览Excel数据")
    @PostMapping("/preview")
    @PreAuthorize("hasAuthority('wide_table:view')")
    public Result<List<Map<String, Object>>> previewExcelData(
            @ApiParam("Excel文件") @RequestParam("file") MultipartFile file,
            @ApiParam("最大行数") @RequestParam(defaultValue = "10") int maxRows) {
        
        List<Map<String, Object>> data = excelImportService.previewExcelData(file, maxRows);
        return Result.success("预览成功", data);
    }

    @ApiOperation("导入Excel数据")
    @PostMapping("/import")
    @PreAuthorize("hasAuthority('wide_table:create')")
    public Result<String> importExcelData(
            @ApiParam("Excel文件") @RequestParam("file") MultipartFile file,
            @ApiParam("宽表ID") @RequestParam Long wideTableId,
            @ApiParam("导入模式") @RequestParam String importMode,
            @ApiParam("字段映射") @RequestBody Map<String, String> fieldMapping) {
        
        String taskId = excelImportService.importExcelData(file, wideTableId, importMode, fieldMapping);
        return Result.success("导入任务已创建", taskId);
    }

    @ApiOperation("获取导入任务状态")
    @GetMapping("/task/{taskId}")
    @PreAuthorize("hasAuthority('wide_table:view')")
    public Result<ImportTaskStatus> getImportTaskStatus(
            @ApiParam("任务ID") @PathVariable String taskId) {
        
        ImportTaskStatus status = excelImportService.getImportTaskStatus(taskId);
        return Result.success(status);
    }

    @ApiOperation("取消导入任务")
    @DeleteMapping("/task/{taskId}")
    @PreAuthorize("hasAuthority('wide_table:create')")
    public Result<Boolean> cancelImportTask(
            @ApiParam("任务ID") @PathVariable String taskId) {
        
        boolean cancelled = excelImportService.cancelImportTask(taskId);
        return Result.success("取消成功", cancelled);
    }
}